<include file="Public/head"/>
	<script src="__PUBLIC__/qwadmin/js/kalendae.standalone.js"></script>
	<css href="__PUBLIC__/qwadmin/css/kalendae.css"/>
<style type="text/css" media="screen">
		.kalendae .k-days span.closed {
			background:red;
		}
	</style>
<body class="no-skin">
<include file="Public/header"/>
<div class="main-container" id="main-container">
    <script type="text/javascript">
        try {
            ace.settings.check('main-container', 'fixed')
        } catch (e) {
        }
    </script>

    <include file="Public/sidebar"/>
    <div class="main-content">
        <div class="main-content-inner">
            <!-- #section:basics/content.breadcrumbs -->
            <include file="Public/breadcrumbs"/>

            <!-- /section:basics/content.breadcrumbs -->
            <div class="page-content">
                <include file="Public/set"/>

                <!-- /section:settings.box -->
                <div class="row">
                    <div class="col-xs-12">
                        <!-- PAGE CONTENT BEGINS -->
                        <form class="form-horizontal" id="form" role="form" method="post" action="{:U('add')}">


                            <div class="space-4"></div>
                            <!-- <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">请选择驾考类型</label>
                                <div class="col-sm-9" id="license_type" name='license_type'>
									 <button class="btn btn-primary" value='' type="button" >请选择驾考类型</button>
                                </div>
                            </div> -->
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">请选择教练 </label>
                                <div class="col-sm-9" id="coach" >
									 <button class="btn btn-primary" value='' type="button" >请选择教练</button>
                                </div>
                            </div>
                             <input type="hidden" name="name" id="name"  value="">
                             <input type="hidden" name="coach_id" id="coach_id"  value="">
                             <input type="hidden" name="sc_id" id="sc_id"  value="">
                             <input type="hidden" name="city" id="city"  value="">
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">电话号码</label>
                                <div class="col-sm-5">
                                    <input type="text" name="phone" id="phone"  placeholder='13526576052' class="col-xs-5 col-sm-5" value="">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1"> 驾考类型 </label>
                                <div class="col-sm-9">
                                    <select id="li_type" name="li_type" class="multiselect" onchange="changeCar(this)">
                                    	<option value="" >请选择驾考类型</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1"> 教练车信息 </label>
                                <div class="col-sm-9">
                                    <select id="car_id" name="car_id" class="multiselect" onchange="">
                                    	<option value="" >请选择教练车</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1"> 练习模式（类别）</label>
                                <div class="col-sm-9">
                                    <select id="course" name="category" class="multiselect">
                                        <option value="1" selected="selected">普通练习</option>
                                        <option value="2">模拟考试</option>
                                    </select>
                                </div>
                            </div>
                             <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1"> 科目类别</label>
                                <div class="col-sm-9">
                                    <select id="course" name="course" class="multiselect" onchange="changeSit(this)">
                                    	<option value="" >请选择科目类型</option>
                                        <option value="1" <if condition="$data['course'] eq 1">selected="selected"</if>>科目二</option>
                                        <option value="2" <if condition="$data['course'] eq 2">selected="selected"</if>>科目三</option>
                                    </select>
                                </div>
                            </div>
                           <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">训练场地</label>
                                <div class="col-sm-9">
                                    <select id="site" name="site" class="multiselect" >
                                       <option value="" >请选择场地</option>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" for="id-date-picker-1">预约日期</label>
                                <!-- <div class="col-xs-9 col-sm-9">
                                    <div class="input-group col-xs-5">

                                        <input class="form-control" id="date" name="date" value=""
                                        type="text" data-date-format="dd-mm-yyyy" />
                                        <span class="input-group-addon">
													<i class="ace-icon fa fa-calendar bigger-110"></i>
												</span>

                                    </div>
                                </div> -->
                                <div id ='kalendae'>
                                
                                </div>
                                <script type="text/javascript" charset="utf-8">
									var k = new Kalendae(document.getElementById('kalendae'), {
										months:1,
										mode:'multiple',
										selected:[Kalendae.moment()]
									});
							
									k.subscribe('change', function (date) {
									   console.log(this.getSelected());
									   $('#time').val(this.getSelected());
									});
							
								</script>
								<input type='hidden' name='time' value='' id='time'/>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">时间段类型</label>
                                <div class="col-sm-9" id="slot" name='slot'>
									 <button class="btn btn-primary" value='' type="button" >请选择时间段类型</button>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <label class="col-sm-1 control-label no-padding-right" for="form-field-1">时间段</label>
                                <!-- <label class="checkbox"><input type="checkbox" value=""></label> -->
                                <div id ='app_time' class="col-sm-9">
                                
                                </div>
                            </div>
                            
							<div class='col-sm-6'></div>
                            <div class="col-md-offset-2 col-md-9">
                                <button class="btn btn-info submit" type="button">
                                    <i class="icon-ok bigger-110"></i>
                                    提交
                                </button>

                                &nbsp; &nbsp; &nbsp;
                                <button class="btn" type="reset">
                                    <i class="icon-undo bigger-110"></i>
                                    重置
                                </button>
                            </div>
                        </form>
                        <!-- PAGE CONTENT ENDS -->
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.page-content -->
        </div>
    </div><!-- /.main-content -->
    <include file="Public/footer"/>

</div><!-- /.main-container -->

<include file="Public/footerjs"/>


<!-- inline scripts related to this page -->

<script type="text/javascript">

$(".submit").click(function () {
	   var 	phone =$('#phone').val(); 
		 if(phone == ''){
	         bootbox.dialog({
	             title: '友情提示：',
	             message: "请填写手机号",
	             buttons: {
	                 "success": {
	                     "label": "确定",
	                     "className": "btn-danger"
	                 }
	             }
	         });
	         return;
	     }
		 
		 var course =$('#course').val(); 
		 if(course == ''){
	         bootbox.dialog({
	             title: '友情提示：',
	             message: "请选择科目类别",
	             buttons: {
	                 "success": {
	                     "label": "确定",
	                     "className": "btn-danger"
	                 }
	             }
	         });
	         return;
	     }
		 
		 var site =$('#site').val(); 
		 if(site == ''){
	         bootbox.dialog({
	             title: '友情提示：',
	             message: "请选择场地",
	             buttons: {
	                 "success": {
	                     "label": "确定",
	                     "className": "btn-danger"
	                 }
	             }
	         });
	         return;
	     }
		 
		 var time =$('#time').val(); 
		 if(time == ''){
	         bootbox.dialog({
	             title: '友情提示：',
	             message: "请选择预约日期",
	             buttons: {
	                 "success": {
	                     "label": "确定",
	                     "className": "btn-danger"
	                 }
	             }
	         });
	         return;
	     }
		 var chk_value =[]; 
		 $('input[name="slot_id[]"]:checked').each(function(){ 
		 chk_value.push($(this).val()); 
		 }); 
		 console.log(chk_value);
		 if(chk_value.length == 0){
	         bootbox.dialog({
	             title: '友情提示：',
	             message: "请选择预约时间段",
	             buttons: {
	                 "success": {
	                     "label": "确定",
	                     "className": "btn-danger"
	                 }
	             }
	         });
	         return;
	     }
		 
		 
		 //检验同一个教练在同一天是否重复排班,需传参，教练id及日期
	  	var coach_id =$('#coach_id').val();
		 console.log(coach_id,time);
		 $.ajax({
				type:'post',
				url:"{:U('Appointment/checkCoach')}",
				async:false,
				data:{coach_id:coach_id,time:time},
				dateType:'json',
				success:function(data){
					var data = eval('('+data+')');
					 if(data.res == 0){
						 $("#form").submit();
					 }else if(data.res ==1){
						 bootbox.dialog({
				             title: '友情提示：',
				             message: data.msg,
				             buttons: {
				                 "success": {
				                     "label": "确定",
				                     "className": "btn-danger"
				                 }
				             }
				         });
				         return false;
					 }else if(data.res == 2){
						 bootbox.confirm({  
						        buttons: {  
						            confirm: {  
						                label: '确认重复排班',  
						                className: 'btn-default'  
						            },  
						            cancel: {  
						                label: '取消重复排班',  
						                className: 'btn-danger'  
						            }  
						        },  
						        title: "警告：",  
						        message: data.msg,  
						        callback: function(result) {  
						            if(result) {  
						            	 $("#form").submit();
						            } else {  

						            }  
						        }  
						        });
					 }
				}
				
			});

	})

	    //默认加载该驾校下所有教练信息
	     $(function(){
	    	 //加载教练信息
	    	$.ajax({
				type:'post',
				url:"{:U('selectCoach')}",
				async:false,
				data:'',
				dateType:'json',
				success:function(data){
					var data = eval('('+data+')');
					var str = '';
					if(data.res == 1){
						console.log(data);
						for (var i in data.data){
							//console.log(data.data);
							str=str+'<button class="btn btn-primary" value='+data.data[i]['id']+' type="button" onclick="getValue(this.value,$(this))" >'+data.data[i]['name']+'</button>&nbsp;&nbsp;';
						}
						//console.log(str);
						$("#coach").html(str);
					}else if(data.res == 0){
						
						var estr= '<button class="btn btn-primary">未找到教练员</button>';
						$("#coach").html(estr);
						bootbox.dialog({
		                    title: '友情提示：',
		                    message: data.msg,
		                    buttons: {
		                        "success": {
		                            "label": "确定",
		                            "className": "btn-danger"
		                        }
		                    }
		                });

					}else{
						
						var estr= '<button class="btn btn-primary">未找到教练员</button>';
						$("#coach").html(estr);
						 bootbox.dialog({
			                    title: '友情提示：',
			                    message: "未找到驾考类型信息",
			                    buttons: {
			                        "success": {
			                            "label": "确定",
			                            "className": "btn-danger"
			                        }
			                    }
			                });
			                return false;
					}
				}
				
			});
	    	 	 
	    	 //加载时间段类型
	    	$.ajax({
				type:'post',
				url:"{:U('TimeSlot/selectSlot')}",
				async:false,
				data:'',
				dateType:'json',
				success:function(data){
					var data = eval('('+data+')');
					var str = '';
					if(data.res == 1){
						//console.log(data);
						for (var i in data.data){	
							str=str+'<button class="btn btn-primary" value='+data.data[i]['id']+' type="button" onclick="getSlot(this.value,$(this))" >'+data.data[i]['slot_name']+'</button>&nbsp;&nbsp;';
						}
						//console.log(str);
						$("#slot").html(str);
						appTime(data.data[0]['id']);
					}else{
						var estr= '<button class="btn btn-primary">未找到时间段类型</button>';
						$("#slot").html(estr);
						 bootbox.dialog({
			                    title: '友情提示：',
			                    message: "未找到时间段类型信息",
			                    buttons: {
			                        "success": {
			                            "label": "确定",
			                            "className": "btn-danger"
			                        }
			                    }
			                });
			                return false;
					}
				}
				
			}); 
	    	
	    })
	    //
	    function  getSlot(val,_this){
	    	var slot_id = val;
	    	 _this.attr('class','btn btn-success').siblings().attr("class","btn btn-primary");
	    	 console.log(slot_id);
	    	 appTime(slot_id);
	    }
	    function  getValue(val,_this){
	    	var coach_id = val;
	    	 _this.attr('class','btn btn-success').siblings().attr("class","btn btn-primary");
	    	 console.log(coach_id);
	    	 coachInfo(coach_id);
	        // _this.css("background","#ffff00").siblings().css("background","#ffffff");
	    }
	    //根据slot_id 查找到时间段信息
	    function  appTime(slot_id){
	    	var slot_id = slot_id;
	    	if(slot_id==''){
	    		  bootbox.dialog({
	                  title: '友情提示：',
	                  message: "请选择时间段类别",
	                  buttons: {
	                      "success": {
	                          "label": "确定",
	                          "className": "btn-danger"
	                      }
	                  }
	              });
	    		  return false;
	    	  }
	    	  $.ajax({
	    			type:'post',
	    			url:"{:U('TimeSlot/appTime')}",
	    			async:false,
	    			data:{'slot_id':slot_id},
	    			dateType:'json',
	    			success:function(data){
	    				var data = eval('('+data+')');
	    				var str = '';
	    				//console.log(data);
	    				if(data.res == 1){
	    					for(var i in data.data){
	    						str = str+'<label class="checkbox  col-sm-9  col-xs-8 "><input type="checkbox" class="slot_id" name="slot_id[]" value='+data.data[i]['id']+'>'+data.data[i]['time']+'价格：'+data.data[i]['price']+'元</label>';
	    							}
	    					$("#app_time").html(str);
	    				}else{
	    					 bootbox.dialog({
	    		                    title: '友情提示：',
	    		                    message: '未查到时间段信息，请先添加',
	    		                    buttons: {
	    		                        "success": {
	    		                            "label": "确定",
	    		                            "className": "btn-danger"
	    		                        }
	    		                    }
	    		                });
	    		                return false;
	    				}
	    			}
	    			
	    		});
	    }
	    
	    //教练的详细信息
	    function coachInfo(coach_id){
	    	//console.log(coach_id);
	    	if(coach_id==''){
	  		  bootbox.dialog({
	                title: '友情提示：',
	                message: "请选择教练",
	                buttons: {
	                    "success": {
	                        "label": "确定",
	                        "className": "btn-danger"
	                    }
	                }
	            });
	  		  return false;
	  	  }
	  	  $.ajax({
	  			type:'post',
	  			url:"{:U('coachInfo')}",
	  			async:false,
	  			data:{'coach_id':coach_id},
	  			dateType:'json',
	  			success:function(data){
	  				var data = eval('('+data+')');
	  				var str = '';
	  				//console.log(data);
	  				if(data.res == 1){
	  					console.log(data.data);
	  					$('#name').val(data.data.name);
	  					$('#coach_id').val(data.data.id);
	  					$('#sc_id').val(data.data.d_id);
	  					$('#phone').val(data.data.phone);
	  					$('#li_type').val(data.data.type);
	  					$('#car_num').val(data.data.car_num);
	  					$('#city').val(data.data.city);
	  					liceseType(data.data.d_id,data.data.driver_type);
	  					carNum(data.data.d_id,data.data.car_id,data.data.driver_type);
	  					//$("#coach").html(str);
	  					
	  				}else{
	  					 bootbox.dialog({
	  		                    title: '友情提示：',
	  		                    message: '未找到教练信息',
	  		                    buttons: {
	  		                        "success": {
	  		                            "label": "确定",
	  		                            "className": "btn-danger"
	  		                        }
	  		                    }
	  		                });
	  		                return false;
	  				}
	  			}
	  			
	  		});
	    }
	    
	    //驾考类型
	    function liceseType(d_id,driver_type){
	    	 $.ajax({
					type:'post',
					url:"{:U('liceseType')}",
					async:false,
					data:{'d_id':d_id},
					dateType:'json',
					success:function(data){
						var data = eval('('+data+')');
						var str = '<option value="" >请选择驾考类型</option>';
						
						if(data.res == 1){
							//console.log(data.data);
							//console.log(driver_type);
						for(var i in data.data){
							if(data.data[i]['id'] == driver_type){
								str= str +"<option selected='selected' value="+data.data[i]['id']+" >"+data.data[i]['type']+"</option>";
							}else{
								str= str +"<option value="+data.data[i]['id']+" >"+data.data[i]['type']+"</option>";
							}
						}
							
							 $("#li_type").html(str);
						}else{
							 bootbox.dialog({
				                    title: '友情提示：',
				                    message: data.msg,
				                    buttons: {
				                        "success": {
				                            "label": "确定",
				                            "className": "btn-danger"
				                        }
				                    }
				                });
						}
					}
					
				});
	    }
	    //选择车辆信息
	    function carNum(d_id,car_id,l_type){
	    	console.log(d_id,car_id,l_type);
	    	 $.ajax({
					type:'post',
					url:"{:U('carNum')}",
					async:false,
					data:{'d_id':d_id,'l_type':l_type},
					dateType:'json',
					success:function(data){
						var data = eval('('+data+')');
						var str = '<option value="" >请选择车辆</option>';
						
						if(data.res == 1){
							console.log(data.data);
							console.log(car_id);
						for(var i in data.data){
							if(data.data[i]['id'] == car_id){
								str= str +"<option selected='selected' value="+data.data[i]['id']+" >"+data.data[i]['car_num']+"</option>";
							}else{
								str= str +"<option value="+data.data[i]['id']+" >"+data.data[i]['car_num']+"</option>";
							}
						}
						$("#car_id").html(str);
						
							 
						}else{
							 bootbox.dialog({
				                    title: '友情提示：',
				                    message: '未找到车辆信息',
				                    buttons: {
				                        "success": {
				                            "label": "确定",
				                            "className": "btn-danger"
				                        }
				                    }
				                });
						}
					}
					
				});
	    }
	    
	  //根据选择的科目类型，从表中查询训练场地
	  function changeSit(_this){
		  var val = $(_this).val();
		  console.log(val);
		  if(val==''){
	  		  bootbox.dialog({
	                title: '友情提示：',
	                message: "请重新选择科目类别",
	                buttons: {
	                    "success": {
	                        "label": "确定",
	                        "className": "btn-danger"
	                    }
	                }
	            });
	  		  return false;
	  	  }
		  $.ajax({
				type:'post',
				url:"{:U('selectSite')}",
				async:false,
				data:{'course':val},
				dateType:'json',
				success:function(data){
					var data = eval('('+data+')');
					var str = '<option value="" >请选择场地</option>';
					console.log(data);
					if(data.res == 1){
						console.log(data.data);
					for(var i in data.data){
						str= str +"<option value="+data.data[i]['id']+" >"+data.data[i]['site']+"</option>";
					}
						$("#site").html(str);
					}else{
						 bootbox.dialog({
			                    title: '友情提示：',
			                    message: data.msg,
			                    buttons: {
			                        "success": {
			                            "label": "确定",
			                            "className": "btn-danger"
			                        }
			                    }
			                });
					}
				}
				
			});
		  
	  }
//当驾考类型发生变化时，调用此函数	  
	  function changeCar(_this){
		  var val = $(_this).val();
		  console.log(val);
		  if(val==''){
	  		  bootbox.dialog({
	                title: '友情提示：',
	                message: "请重新选择驾考类型",
	                buttons: {
	                    "success": {
	                        "label": "确定",
	                        "className": "btn-danger"
	                    }
	                }
	            });
	  		  return false;
	  	  }
		  $.ajax({
				type:'post',
				url:"{:U('changeCar')}",
				async:false,
				data:{'l_type':val},
				dateType:'json',
				success:function(data){
					var data = eval('('+data+')');
					var str = '<option value="" >请选择教练车</option>';
					console.log(data);
					if(data.res == 1){
						console.log(data.data);
					for(var i in data.data){
						str= str +"<option value="+data.data[i]['id']+" >"+data.data[i]['car_num']+"</option>";
					}
						$("#car_id").html(str);
					}else{
						 bootbox.dialog({
			                    title: '友情提示：',
			                    message: data.msg,
			                    buttons: {
			                        "success": {
			                            "label": "确定",
			                            "className": "btn-danger"
			                        }
			                    }
			                });
						 $("#car_id").html(str);
					}
				}
				
			});
		  
	  }
	  
	    

	    
</script>
</body>
</html>
